package com.qst.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qst.domain.Book;
import com.qst.domain.Record;
import com.qst.domain.User;
import com.qst.entity.PageResult;
import com.qst.mapper.BookMapper;
import com.qst.service.BookService;
import com.qst.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    /*
    根据当前页码和每页需要展示的数据条数,查询最新上架的图书信息
    */
    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer pageSize) {
        // 设置分页查询数据,开始分页
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> books = bookMapper.selectNewBooks();
        return new PageResult(books.getTotal(), books.getResult());
    }

    @Autowired
    private RecordService recordService;

    // 新增设置记录信息方法
    public Record setRecord(Book book) {
        Record record = new Record();
        record.setBookname(book.getName());
        record.setBookisbn(book.getIsbn());
        record.setBorrower(book.getBorrower());
        record.setBorrowTime(book.getBorrowTime());
        // 设置当前时间为图书归还时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        record.setRemandTime(sdf.format(new Date()));
        return record;
    }

    /*
修改归还确认方法
*/
    @Override
    public Integer returnConfirm(String id) {
        // 根据id查询完整图书信息
        Book book = findById(id);
        // 调用设置借阅记录方法获取一个借阅记录对象
        Record record = setRecord(book);
        book.setStatus("0");
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");
        Integer count = bookMapper.editBook(book);
        // 若确认归还,则新增借阅记录
        if (count == 1){
            return recordService.addRecord(record);
        }
        return 0;
    }


    /* 根据id查询图书 */
    @Override
    public Book findById(String id) {
        return bookMapper.findById(id);
    }

    /* 借阅图书 */
    @Override
    public Integer borrowBook(Book book) {
        // 根据id查询出完整的书籍信息
        Book b = this.findById(book.getId().toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 设置当天为借阅时间
        String s = sdf.format(new Date());
        book.setBorrowTime(s);
        // 设置图书状态为借阅中
        book.setStatus("1");
        // 将图书的价格设置在book对象中
        book.setPrice(b.getPrice());
        // 将图书的上架设置在book对象中
        book.setUploadTime(b.getUploadTime());
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        // 设置分页查询参数,开始分页
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> books = bookMapper.searchBooks(book);
        return new PageResult(books.getTotal(), books.getResult());
    }

    @Override
    public Integer addBook(Book book) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 设置新增图书上架时间
        book.setUploadTime(sdf.format(new Date()));
        return bookMapper.addBook(book);
    }

    // 编辑图书信息
    @Override
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }


    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        // 将当前用户放在查询条件中
        book.setBorrower(user.getUsername());
        // 设置分页查询参数
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> books;
        // 根据当前用户的身份,赋值不同的查询结果
        if ("ADMIN".equals(user.getRole())) {
            books = bookMapper.selectBorrowed(book);
        } else {
            books = bookMapper.selectMyBorrowed(book);
        }
        // 封装结果并返回.
        return new PageResult(books.getTotal(), books.getResult());
    }


    /*
        归还图书
    */
    @Override
    public boolean returnBook(String id,User user){
        // 根据图书id查询出完整的图书信息
        Book book = findById(id);
        // 再次核验当前登录人员和图书借阅员是否为同一个人
        boolean b = user.getUsername().equals(book.getBorrower());
        if (b){
            // 若是同一个人,则允许归还
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return b;
    }


}
